<template>
    <el-container>
        <el-header class="homeHeader">
            <div class="title">微人事</div>
            <div>
                <el-button icon="el-icon-bell" type="text" style="color: #fff;margin-right: 8px;"/>
                <el-dropdown @command="handleCommand">
                  <span class="el-dropdown-link">
                    {{hr.name}}<i><img :src="hr.userface"></i>
                  </span>
                    <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item command="hrinfo">个人中心</el-dropdown-item>
                        <el-dropdown-item command="settings">设置</el-dropdown-item>
                        <el-dropdown-item divided command="logout">注销登录</el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>
            </div>
        </el-header>
        <el-container>
            <el-aside width="200px">
                <el-menu unique-opened router>
                    <el-submenu :index="index + ''" v-for="(item,index) in menus" :key="item.id">
                        <template slot="title">
                            <i :class="item.iconcls"></i>
                            <span>{{item.name}}</span>
                        </template>
                        <el-menu-item :index="i.path" v-for="i in item.children" :key="i.id">{{i.name}}</el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <el-main>
                <template v-if="this.$router.currentRoute.path == '/welcome'">
                    <div class="homeWelcome">欢迎来到微人事！</div>
                </template>
                <template v-else>
                    <el-breadcrumb separator-class="el-icon-arrow-right">
                        <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
                        <el-breadcrumb-item>{{this.$router.currentRoute.name}}</el-breadcrumb-item>
                    </el-breadcrumb>
                </template>
                <router-view/>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
    export default {
        name: "Welcome",
        data: function () {
            return {
                menus: []
            }
        },
        methods: {
            handleCommand: function (command) {
                switch (command) {
                    case 'hrinfo':
                        // 组件切换
                        this.$router.push('/hrinfo')
                        break
                    case 'settings':
                        break
                    case 'logout':
                        this.$getRequest('/logout')
                            .then(res => {
                                if (res) {
                                    sessionStorage.removeItem('current_hr')
                                    this.$router.replace('/')
                                }
                            })
                        break
                }
            }
        },
        mounted() {
            this.$getRequest('/vhr/menu/')
                .then(res => {
                    if (res){
                        this.menus = res.obj
                    }
                })
        },
        computed:{
            hr(){
                return this.$store.state.hr
            }
        }
    }
</script>

<style scoped>
    .homeRouterView {
        margin-top: 10px;
    }

    .homeWelcome {
        text-align: center;
        font-size: 30px;
        font-family: 华文行楷;
        color: #409eff;
        padding-top: 50px;
    }

    .homeHeader {
        background-color: #409eff;
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 0px 15px;
        box-sizing: border-box;
    }

    .homeHeader .title {
        font-size: 30px;
        font-family: 华文行楷;
        color: #ffffff
    }

    .homeHeader .userInfo {
        cursor: pointer;
    }

    .el-dropdown-link img {
        width: 48px;
        height: 48px;
        border-radius: 24px;
        margin-left: 8px;
    }

    .el-dropdown-link {
        display: flex;
        align-items: center;
        cursor: pointer;
        color: #fff;
    }
</style>